
package com.kweative.model;

import com.kweative.model.exception.ColumnNotFoundException;
import com.kweative.model.exception.NotAddedToTableException;
import com.kweative.model.column.Column;
import com.kweative.model.column.IColumn;
import java.util.ArrayList;


/**
 *
 * @author henrik
 */
public class Index extends Object {
    private Type type;
    private String name;
    private ArrayList<IColumn> cols = new ArrayList<IColumn>();
    private Table table = null;

    public Index(String name,Type type) {
        this.type = type;
        this.name = name;
    }
    public Index(String name) {
        this(name,Type.INDEX);
    }
	public Index(Type type) {
        this("",type);
    }

    public String getName() {
        return name;
    }

	public void setName(String name) {
		this.name = name;
	}
	

    public Type getType() {
        return type;
    }
    
    
    public IColumn addColumn(IColumn col) throws NotAddedToTableException, ColumnNotFoundException {
        if (table == null)
            throw new NotAddedToTableException(this);
//        if (!table.hasColumn(col))
//            throw new ColumnNotFoundException(col, table);
        cols.add(col);
        return col;
    }
	public <C extends Column> C addColumn(C col) throws NotAddedToTableException, ColumnNotFoundException {
		addColumn((IColumn)col);
		return col;
	}

    public Table getTable() {
        return table;
    }

    public void setTable(Table table) {
        this.table = table;
    }

    public ArrayList<IColumn> getColumns() {
        return cols;
    }


    public enum Type {
        PRIMARY,
        TEXT,
        INDEX,
        UNIQUE
    }
}
